package com.example.sunsg.okhttptest.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.customview.SlideRemoveLib;

import java.util.ArrayList;
import java.util.List;

public class SlideRemoveActivity extends AppCompatActivity {
    private ListView listView;
    private List<String> mDatas = new ArrayList<String>();
    private MyAdapter myAdapter;
    private SwipeRefreshLayout mRefreshLayout;
    private Handler mHandler;

    public static void launch(Context context){
        Intent intent = new Intent(context,SlideRemoveActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide_remove);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        initView();

        initData();
    }

    private void initData() {

        for(int i=0;i<10;i++){
            mDatas.add("andy" + i);
        }
        myAdapter.notifyDataSetChanged();

    }

    private void initView() {

        listView = (ListView) findViewById(R.id.list);
        myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);
        mHandler = new Handler();
        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mRefreshLayout.setRefreshing(false);
                    }
                }, 3000);
            }
        });

    }

    private class ViewHolder{
        SlideRemoveLib sl_test ;
        TextView content;
        TextView delete;
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder vHolder = null;

            if(convertView == null){
                convertView = View.inflate(getApplicationContext(), R.layout.item_slide_activity, null);
                vHolder  = new ViewHolder();

                vHolder.sl_test = (SlideRemoveLib) convertView.findViewById(R.id.srl_remove);
                vHolder.content = (TextView) convertView.findViewById(R.id.tv_content);
                vHolder.delete = (TextView) convertView.findViewById(R.id.tv_delete);

                convertView.setTag(vHolder);

            }else{
                vHolder = (ViewHolder) convertView.getTag();
            }


            String msg = getItem(position);
            vHolder.content.setText(msg);

            final ViewHolder finalViewHolder = vHolder;

            vHolder.delete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mDatas.remove(position);
                    finalViewHolder.sl_test.noShowDeleteView();
                    myAdapter.notifyDataSetChanged();
                }
            });


            return convertView;
        }



        @Override
        public String getItem(int position) {
            return mDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }



    }



}
